package org.jeecg.modules.medicine.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.medicine.entity.MedicineCate;
import org.jeecg.modules.medicine.service.MedicineCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 药品类别表
 * @Author: 孔小波
 * @Date:   2020-01-09
 * @Version: V1.0
 */
@RestController
@RequestMapping("/medicine/medicineCate")
@Slf4j
public class MedicineCateController extends JeecgController<MedicineCate, MedicineCateService> {
    @Autowired
    private MedicineCateService medicineCateService;
    /**
     * 分页列表查询
     *
     * @param medicineCate
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(MedicineCate medicineCate,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<MedicineCate> queryWrapper= QueryGenerator.initQueryWrapper(medicineCate,req.getParameterMap());
        Page<MedicineCate> page = new Page<MedicineCate>(pageNo, pageSize);
        IPage<MedicineCate> pageList=medicineCateService.page(page,queryWrapper);
        return Result.ok(pageList);
    }

    /**
     *   添加
     *
     * @param medicineCate
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MedicineCate medicineCate) {
        System.out.println(medicineCate.toString());
        medicineCateService.save(medicineCate);
        return Result.ok("添加成功！");
    }

    /**
     *  编辑
     *
     * @param medicineCate
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody MedicineCate medicineCate) {
        medicineCateService.updateById(medicineCate);
        return Result.ok("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        medicineCateService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.medicineCateService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        MedicineCate medicineCate = medicineCateService.getById(id);
        if(medicineCate==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(medicineCate);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param medicineCate
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MedicineCate medicineCate) {
        return super.exportXls(request, medicineCate, MedicineCate.class, "药品类别表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MedicineCate.class);
    }

}
